{% extends "base_generic.html" %}
{% load i18n %}

{% block content %}
  <h1>{% if form.instance.pk %}{% translate "Update Book" %}{% else %}{% translate "Create Book" %}{% endif %}</h1>
  
  <form action="" method="post" novalidate>
    {% csrf_token %}
    
    {% for field in form %}
      <div class="mb-3">
        <label for="{{ field.id_for_label }}" class="form-label">{{ field.label }}</label>
        
        {% if field.name == 'genre' %}
          {# Special handling for CheckboxSelectMultiple for Bootstrap 5 #}
          <div id="{{ field.id_for_label }}_container"> {# Ensure unique ID for the container if field.id_for_label is for the first input #}
            {% for choice in field %}
            <div class="form-check">
              {{ choice.tag }}
              <label class="form-check-label" for="{{ choice.id_for_label }}">
                {{ choice.choice_label }}
              </label>
            </div>
            {% endfor %}
          </div>
        {% else %}
          {# Default rendering for other fields #}
          {{ field }}
        {% endif %}
        {% if field.help_text %}
          <small class="form-text text-muted">{{ field.help_text|safe }}</small>
        {% endif %}
        {% for error in field.errors %}
          <div class="invalid-feedback d-block">
            {{ error }}
          </div>
        {% endfor %}
      </div>
    {% endfor %}
    
    <button type="submit" class="btn btn-primary">{% translate "Submit" %}</button>
    <a href="{% if form.instance.pk %}{{ form.instance.get_absolute_url }}{% else %}{% url 'books' %}{% endif %}" class="btn btn-secondary">{% translate "Cancel" %}</a>
  </form>
{% endblock %}
